package com.yz.lzq.liwushuo.ui.modules.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.jsonbean.HomeFragmentTabLayoutBean;
import com.yz.lzq.liwushuo.ui.modules.home.boutique.HomeBoutiqueFragment;
import com.yz.lzq.liwushuo.ui.modules.home.nomal.HomeNomalFragment;
import com.yz.lzq.liwushuo.ui.setting.SettingActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Lzq on 2016/11/12.
 */

public class HomeFragment extends Fragment implements HomeContract.HomeFrgmentViewInterface {
    @Bind(R.id.tabLayout_homefragment)
    TabLayout tabLayoutHomefragment;
    @Bind(R.id.imageView_homefragment_arrowDown)
    ImageView imageViewHomefragmentArrowDown;
    @Bind(R.id.imageView_homefragment_arrowUp)
    ImageView imageViewHomefragmentArrowUp;
    @Bind(R.id.viewPager_homeFragment)
    ViewPager viewPagerHomeFragment;
    private Context mContext = null;
    private XRecyclerView recyvleView_home;
    List<String> mStringList = new ArrayList<>();
    //viewpager适配器
    private HomeFragmentPagerAdapter homeFragmentPagerAdapter = null;
    //填充fragment的数据源
    private List<Fragment> mList_fragment = new ArrayList<>();
    private HomePresenter homePresenter;

    /**
     * 初始化Homefragment对象的单例模式
     *
     * @return 当前fragment对象
     */
    public static HomeFragment getInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    /**
     * @param inflater           布局加载器
     * @param container          父容器
     * @param savedInstanceState 保存状态的信息
     * @return 返回加载布局
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, view);
        return view;
    }

    /**
     * 初始化控件(黄油刀替代了)
     *
     * @param view               加载过得布局
     * @param savedInstanceState 保存状态的信息
     */
   /* @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }*/

    /**
     * 为控件赋值
     *
     * @param savedInstanceState 获取保存状态的信息
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        homePresenter = new HomePresenter(mContext, this);
        homePresenter.showTabLayoutTittle();
        imageViewHomefragmentArrowDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, SettingActivity.class);
                startActivity(intent);
            }
        });
        /*
        tabLayoutHomefragment.setupWithViewPager(viewPagerHomeFragment);
        tabLayoutHomefragment.addTab(tabLayoutHomefragment.newTab().setText("精品"), 0);
   */
    }

    /**
     * 视图销毁
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 碎片销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void ShowTabLayoutTittle(HomeFragmentTabLayoutBean bean) {
        Log.i("tag", "ShowTabLayoutTittle==============>>>>>>>>>: "+bean.getData());
        HomeBoutiqueFragment fragment = HomeBoutiqueFragment.getInstance();
        mList_fragment.add(fragment);
        for (int i = 0; i < bean.getData().getCandidates().size(); i++) {
            HomeNomalFragment homeNomalFragment = HomeNomalFragment.getInstance(bean.getData().getCandidates().get(i).getId());
            mList_fragment.add(homeNomalFragment);
        }
        homeFragmentPagerAdapter = new HomeFragmentPagerAdapter(getChildFragmentManager(), bean.getData().getCandidates(), mList_fragment);
        tabLayoutHomefragment.setupWithViewPager(viewPagerHomeFragment);
        viewPagerHomeFragment.setAdapter(homeFragmentPagerAdapter);

    }
}
